#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef WIN32
#include <io.h>
#define F_OK 0
#define access _access
#endif

#include "../src/defines.h"

void printHelp();

// *Make compiler happy*
char filepath[0];

int main(int argc, char** argv) {
    if(argc > 2)
        printHelp();
    else if(argc == 2 && access(argv[1], F_OK) != 0)
        printHelp();
    FILE *file;
    if(argc == 2)
        file = fopen(argv[1], "r");
    else
        file = stdin;
    int c;
    int inWordIndex = 0;
    char word[10];
    enum Genome genome;
    while((c = getc(file)) != EOF) {
        if(c == ' ') {
            word[inWordIndex+1] = '\0';
            genome = atoi(word);
            switch (genome) {
            case(G_SUN_ENERGY):
                puts("Sun energy");
                break;
            case(G_COLLECT_MINERALS):
                puts("Collect minerals");
                break;
            case(G_MINERALS_ENERGY):
                puts("Convert minerals to energy");
                break;
            case(G_MOVE):
                puts("Move");
                break;
            case(G_ROTATE):
                puts("Rotate");
                break;
            case(G_RUP):
            case(G_UP):
            case(G_LUP):
            case(G_LEFT):
            case(G_LDOWN):
            case(G_DOWN):
            case(G_RDOWN):
            case(G_RIGHT):
                puts("Fixed rotation");
                break;
            case(G_CHECK_CELL):
                puts("Check if the cell looks at a cell");
                break;
            case(G_CHECK_WALL):
                puts("Check if the cell looks at a wall");
                break;
            case(G_CHECK_MINWALL):
                puts("Check if the cell looks at a wall from minerals");
                break;
            case(G_CHECK_FOOD):
                puts("Check if the cell looks at a food");
                break;
            case(G_MAKE_CELL):
                puts("Make another cell");
                break;
            case(G_KILL_CELL):
                puts("Kill a cell");
                break;
            case(G_EAT_FOOD):
                puts("Eat a food");
                break;
            case(G_JUMP_GENOME):
                puts("Skip N genome cells");
                break;
            case(G_CHECK_GENOME):
                puts("Compare genomes of the cell and a cell");
                break;
            case(G_SHARE_ENERGY):
                puts("Share energy with a cell");
                break;
            case(G_BUILD_WALL):
                puts("Build wall from minerals");
                break;
            case(G_BREAK_WALL):
                puts("Hit a wall(3 hits will break the wall)");
                break;
            case(G_INFECT):
                puts("Put genome to a cell genome");
                break;
            case(G_CHECK_RED):
            case(G_CHECK_GREEN):
            case(G_CHECK_BLUE):
                puts("Check a cell color");
                break;
            case(G_USE_MINERALS):
                puts("Enable use of minerals to improve sun energy collection");
                break;
            case(G_DONT_USE_MINERALS):
                puts("Disable use of minerals to improve sun energy collection");
                break;
            case(G_END):
            default:
                puts("(NO COMMAND)");
                break;
            }
            printf("%d\n", genome);
            puts("-----");
            inWordIndex = 0;
        } else {
            word[inWordIndex] = c;
            inWordIndex++;
        }
    }
    return(0);
}

void printHelp() {
    printf("Usage:\vdisassemble [file]\nIf file not passed, using stdin\n");
    exit(1);
}
